package cn.tedu.ivos.application.controller;

import cn.tedu.ivos.application.pojo.dto.ApplicationQuery;
import cn.tedu.ivos.application.pojo.dto.ApplicationSaveParam;
import cn.tedu.ivos.application.pojo.vo.ApplicationVO;
import cn.tedu.ivos.application.service.ApplicationService;
import cn.tedu.ivos.base.response.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Validated
@Slf4j
@RestController
@RequestMapping("/v1/application/")
public class ApplicationController {
    @Autowired
    ApplicationService applicationService;


    @ApiOperation(value = "保存用车申请")
    @PostMapping("save")
    public JsonResult save(@Validated ApplicationSaveParam applicationSaveParam) {
        log.debug("开始处理【保存用车申请】的请求，参数：{}", applicationSaveParam);
        applicationService.save(applicationSaveParam);
        return JsonResult.ok();
    }

    @ApiOperation(value = "查询所有申请单")
    @GetMapping("select")
    public JsonResult select(@Validated ApplicationQuery applicationQuery) {
        log.debug("开始处理【查询所有申请单】的请求，参数：{}", applicationQuery);
        List<ApplicationVO> list = applicationService.selectAll(applicationQuery);
        return JsonResult.ok(list);
    }

    @ApiOperation(value = "撤销申请单")
    @PostMapping("cancel/{id}")
    public JsonResult cancel(@PathVariable Long id) {
        log.debug("开始处理【撤销申请单】的请求，参数：{}", id);
        applicationService.cancel(id);
        return JsonResult.ok();
    }

    @ApiOperation(value = "分配用车")
    @PostMapping("distribute")
    public JsonResult distribute( Long id, Long vehicleId) {
        log.debug("开始处理【分配用车】的请求，id={}, vehicleId={}",id,vehicleId);
        applicationService.distribute(id,vehicleId);
        return JsonResult.ok();
    }

    @ApiOperation(value = "还车")
    @PostMapping("back")
    public JsonResult returnVehicle(Long id, Long vehicleId) {
        log.debug("开始处理【还车】的请求，id={}, vehicleId={}",id,vehicleId);
        applicationService.back(id,vehicleId);
        return JsonResult.ok();
    }
}
